/** * Google Identity Services + email OTP (page and/or modal instances). */ (function () { 'use strict'; var cfg = window.travelgoWcAuth; if (!cfg || !cfg.restUrl || !cfg.instances || !cfg.instances.length) { return; } function msg(el, text, isError) { if (!el) { return; } el.hidden = !text; el.textContent = text || ''; el.classList.toggle('is-error', !!isError); } function post(path, body) { var payload = Object.assign({}, body || {}, { travelgo_nonce: cfg.nonce, redirect_to: window.location.href.split('#')[0], }); return fetch(cfg.restUrl.replace(/\/?$/, '/') + path.replace(/^\//, ''), { method: 'POST', credentials: 'same-origin', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(payload), }).then(function (r) { return r.json().then(function (data) { if (!r.ok) { var err = (data && data.message) || cfg.strings.error; throw new Error(err); } return data; }); }); } function redirectTo(data) { if (data && data.redirect) { window.location.href = data.redirect; } else { window.location.reload(); } } function rememberForInstance(inst) { var root = inst.context === 'modal' ? document.getElementById('travelgo-auth-modal') : document.body; if (!root) { return false; } var el = root.querySelector('input[name="rememberme"]'); return !!(el && el.checked); } function initGoogle(inst) { if (!cfg.googleClientId || !window.google || !google.accounts || !google.accounts.id) { return; } var host = document.getElementById(inst.googleHostId); if (!host) { return; } var statusEl = document.getElementById(inst.statusId); google.accounts.id.initialize({ client_id: cfg.googleClientId, callback: function (res) { if (!res || !res.credential) { return; } msg(statusEl, cfg.strings.working, false); post('auth/google', { credential: res.credential, remember: rememberForInstance(inst), }) .then(redirectTo) .catch(function (e) { msg(statusEl, e.message || cfg.strings.error, true); }); }, }); var w = host.parentElement ? host.parentElement.clientWidth : 400; if (w < 200) { w = Math.min(400, window.innerWidth - 32); } google.accounts.id.renderButton(host, { theme: 'outline', size: 'large', width: w, text: 'signin_with', shape: 'rectangular', }); } function bindOtp(inst) { if (!cfg.otpEnabled) { return; } var sendBtn = document.getElementById(inst.otpSendId); var verifyBtn = document.getElementById(inst.otpVerifyId); var emailEl = document.getElementById(inst.otpEmailId); var codeEl = document.getElementById(inst.otpCodeId); var statusEl = document.getElementById(inst.statusId); if (sendBtn && emailEl) { sendBtn.addEventListener('click', function () { msg(statusEl, '', false); post('auth/otp/request', { email: emailEl.value.trim() }) .then(function (data) { msg(statusEl, (data && data.message) || cfg.strings.otpSent, false); }) .catch(function (e) { msg(statusEl, e.message || cfg.strings.error, true); }); }); } if (verifyBtn && emailEl && codeEl) { verifyBtn.addEventListener('click', function () { msg(statusEl, cfg.strings.working, false); post('auth/otp/verify', { email: emailEl.value.trim(), code: codeEl.value.trim(), remember: rememberForInstance(inst), }) .then(redirectTo) .catch(function (e) { msg(statusEl, e.message || cfg.strings.error, true); }); }); } } function bootInstance(inst) { bindOtp(inst); if (!cfg.googleClientId || inst.context === 'modal') { return; } initGoogle(inst); } function boot() { cfg.instances.forEach(bootInstance); } function bootModalLater() { var modalInst = cfg.instances.filter(function (i) { return i.context === 'modal'; })[0]; if (!modalInst || !cfg.googleClientId) { return; } window.addEventListener('travelgo:auth-modal-opened', function () { var host = document.getElementById(modalInst.googleHostId); if (host && host.getAttribute('data-tg-google-inited') === '1') { return; } window.setTimeout(function () { if (host) { host.setAttribute('data-tg-google-inited', '1'); } initGoogle(modalInst); }, 80); }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', boot); } else { boot(); } bootModalLater(); })();